package com.dpp.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.dpp.entity.OrderDetail;
import com.dpp.entity.Orders;
import com.dpp.entity.TakeOrders;
import com.dpp.vo.HomeVO;
import org.apache.ibatis.annotations.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper extends BaseMapper<Orders> {


    @Select("select * from orders where id = #{id}")
    Orders selectById(Integer id);
    @Select("select * from orders where number = #{number}")
    Orders selectByNumber(Long number );
    @Select("select * from orders  where openid = #{openid} and order_status != -1 and order_status != 0 ")
    List<Orders> selectByOpenIdAll(String openid);

    @Select("select * from orders  where order_status = 1 and product_id not in (3,4,5,6,7,8,13)")
    List<Orders> selectByOpenIdAll1();

    /**
     * 获取历史订单信息
     *
     * @param openid
     * @param id
     * @return
     */

    @Select("select * from orders  where openid = #{openid} and order_status != -1 and order_status != 0 and product_id=#{id}")
    List<Orders> selectByOpenId(String openid, Integer id);
    @Select("select t1.id,t1.number,t1.product_id,t1.order_status,t1.amount,t1.create_time,t1.error_msg,t1.expect_time,t1.finish_time,t1.imageOver,t2.phone,t2.take_address,t2.address," +
            "t2.package_info,t2.remark,t2.image ,t3.order_type,t1.count from orders t1,orders_detail t2 ,product_info t3 where t1.openid = #{openid} and t1.id=t2.order_id and t1.product_id=t3.id and t1.order_status in (1,2,3,4,-2,-3,21,22,23,6,24,25,-5)  order by t1.order_status desc , t1.update_time desc ")
    List<Orders> selectOut1(String openid);
    @Select("select t1.id,t1.number,t1.product_id,t1.order_status,t1.amount,t1.create_time,t1.expect_time,t1.finish_time,t1.imageOver,t2.phone,t2.take_address,t2.address," +
            "t2.package_info,t2.remark,t2.image,t3.order_type,t1.count from orders t1,orders_detail t2,product_info t3 where t1.openid = #{openid} and t1.id=t2.order_id and t1.product_id=t3.id and t1.order_status in (0,5) order by t1.update_time desc ")
    List<Orders> selectOut2(String openid);

    @Select("SELECT * FROM take_orders where number = #{number}")
    TakeOrders selectTakePeople(Long number);

    @Select("select t1.id,t1.number,t1.product_id,t1.order_status,t1.amount,t1.create_time,t1.expect_time,t1.finish_time,t1.imageOver,t1.count,t2.phone,t2.take_address,t2.address," +
            "t2.package_info,t2.remark,t2.image from orders t1,orders_detail t2 where t1.id = t2.order_id and t1.number = #{number} ")
    List<Orders> selectTakes(Long number);

    @Select("select t1.id,t1.number,t1.product_id,t1.order_status,t1.amount,t1.create_time,t2.phone,t2.address,t2.package_info,t2.remark,t2.image,"+
            "t3.order_type from orders t1,orders_detail t2,product_info t3 where t1.id = t2.order_id and t1.openid = #{openid} and t1.product_id = t3.id and t1.order_status = 7 order by t1.update_time desc")
    List<Orders> selectRuns(String openid);


    /**
     * 更新订单状态
     * @param id
     * @param cancel
     */
    @Update("update orders set order_status = #{cancel} where id = #{id}")
    void updateCancel(Integer id,Integer cancel);
    @Update("update orders set order_status = #{cancel} where number = #{number}")
    void updateStatus(Long number,Integer cancel);

    @Update("update orders set order_status = #{received} where id = #{id}")
    void updateCollected(Integer id, Integer received);

    @Update("update orders set order_status = #{reached} where id = #{id}")
    void updateReached(Integer id, Integer reached);

    @Update("update orders set order_status = #{completed} where id = #{id}")
    void updateCompleted(Integer id, Integer completed);

    @Update("update orders set update_time = #{now} where id = #{id}")
    void updateTime(Integer id, LocalDateTime now);

    @Update("update orders set finish_time = #{now} where id = #{id}")
    void updateFinishTime(Integer id, LocalDateTime now);

    @Update("update take_orders set take_time = #{now} where number = #{number}")
    void updateTakeTime(Long number, LocalDateTime now);

    @Update("update take_orders set delivery_time = #{now} where number = #{number}")
    void updateDeliveryTime(Long number, LocalDateTime now);

    @Select("select DISTINCT t1.id, t1.product_id, t1.amount, t1.order_status, t1.create_time, t1.expect_time" +
            ",t2.phone,t2.address,t3.order_type from orders t1,orders_detail t2,product_info t3 where t1.id = t2.order_id and t1.product_id=t3.id and t1.order_status = 1 and product_id in (1,2,9,10,11,12,13,14,15,16,17) order by  t1.create_time DESC ")
    List<HomeVO> selectAll();

    @Select("select DISTINCT t1.id, t1.number,t1.product_id, t1.order_status, t1.amount, t1.create_time, t2.phone, t2.address, t2.package_info, t2.remark,t2.image,"+
            "t3.order_type from orders t1,orders_detail t2,product_info t3 where t1.id = t2.order_id and t1.product_id = t3.id and t1.order_status = 7 and t1.product_id in (18,19,20,21,22) order by t1.create_time desc")
    List<HomeVO> selectAllPost();

    void insertToOrders(Orders orders);

    @Update("update orders set imageOver = #{imageOver} where id=#{id}")
    void updatetImage(Integer id, String imageOver);

    @Insert("insert into orders_detail(order_id, take_address, package_info, remark, phone, address, image) " +
            "values (#{orderId},#{takeAddress},#{packageInfo},#{remark},#{phone},#{address},#{image})")
    void insertToOrderDetail(OrderDetail orderDetail);

    @Update("update orders set order_status = 2 where id = #{id}")
    void updateOrderType(Integer id);

    @Select("select * from orders where order_status = #{reached} and update_time < #{time}")
    List<Orders> selectOutTime(Integer reached, LocalDateTime time);

    @Update("update orders set order_status = #{orderStatus},update_time = #{updateTime},finish_time=#{finishTime} where id = #{id}")
    void updateOrders(Orders orders);
    @Select("select DISTINCT t1.id, t1.product_id, t1.amount, t1.order_status, t1.create_time, t1.expect_time" +
            ",t2.phone,t2.address,t3.order_type from orders t1,orders_detail t2, product_info t3 where t1.id = t2.order_id and t1.product_id=t3.id and t1.order_status not in (0,-1,-3,1,-5,7,8)  and product_id in (1,2,7,8,9,10,11,12,13,14,15,16,17) order by  t1.create_time DESC")
    List<HomeVO> selectEndAll();


    @Update("update orders  set create_time=#{time1} , update_time=#{time1} where  id=#{id}")
    void updata(Integer id, LocalDateTime time1);
    @Select("SELECT * FROM orders t1,take_orders t2 WHERE  t1.number=t2.number and t2.openid=#{openid} and t1.order_status in (2,3)")
    List<Orders> selectByopenId5(String openid);
    @Select("select DISTINCT t1.id, t1.product_id, t1.amount, t1.order_status, t1.create_time, t1.expect_time" +
            ",t2.phone,t2.address,t3.order_type from orders t1,orders_detail t2,product_info t3 where t1.id = t2.order_id and t1.product_id=t3.id and t1.order_status = 1  and product_id  in (7,8) order by  t1.create_time DESC ")
    List<HomeVO> selectUrgent();
}
